<?php
/*
use: remainTime("2011-09-20 16:20",'%d Days, %h Hours, %m Minutes')
*/
function remainTime($timestamp, $format) {
	$now = convertTimeToLocal( date("Y-m-d H:i:s") , date("O")/100 );		
	$difference = strtotime($timestamp) - strtotime($now); 
	if($difference < 0) 
		return false; 
	else{ 
	
		$min_only = intval(floor($difference / 60)); 
		$hour_only = intval(floor($difference / 3600)); 
		
		$days = intval(floor($difference / 86400)); 
		$difference = $difference % 86400; 
		$hours = intval(floor($difference / 3600)); 
		$difference = $difference % 3600; 
		$minutes = intval(floor($difference / 60)); 
		if($minutes == 60){ 
			$hours = $hours+1; 
			$minutes = 0; 
		} 
		/*
		if($days == 0){ 
			$format = str_replace('Days', '?', $format); 
			$format = str_replace('Ds', '?', $format); 
			$format = str_replace('%d', '', $format); 
		} 
		if($hours == 0){ 
			$format = str_replace('Hours', '?', $format); 
			$format = str_replace('Hs', '?', $format); 
			$format = str_replace('%h', '', $format); 
		} 
		if($minutes == 0){ 
			$format = str_replace('Minutes', '?', $format); 
			$format = str_replace('Mins', '?', $format); 
			$format = str_replace('Ms', '?', $format);        
			$format = str_replace('%m', '', $format); 
		} 
		*/
		
		$format = str_replace('?,', '', $format); 
		$format = str_replace('?:', '', $format); 
		$format = str_replace('?', '', $format); 
		
		$timeLeft = str_replace('%d', number_format($days), $format);        
		$timeLeft = str_replace('%ho', number_format($hour_only), $timeLeft); 
		$timeLeft = str_replace('%mo', number_format($min_only), $timeLeft); 
		$timeLeft = str_replace('%h', number_format($hours), $timeLeft); 
		$timeLeft = str_replace('%m', number_format($minutes), $timeLeft); 
			
		if($days == 1){ 
			$timeLeft = str_replace('Days', 'Day', $timeLeft); 
			$timeLeft = str_replace('Ds', 'D', $timeLeft); 
		} 
		if($hours == 1 || $hour_only == 1){ 
			$timeLeft = str_replace('Hours', 'Hour', $timeLeft); 
			$timeLeft = str_replace('Hs', 'H', $timeLeft); 
		} 
		if($minutes == 1 || $min_only == 1){ 
			$timeLeft = str_replace('Minutes', 'Minute', $timeLeft); 
			$timeLeft = str_replace('Mins', 'Min', $timeLeft); 
			$timeLeft = str_replace('Ms', 'M', $timeLeft);            
		} 
			
	  return $timeLeft; 
	} 
} 	


/**Convert time international to local **/
function convertTimeToLocal($time,$timezone)	
{
	global $GMTClient;
	$gmt= strtotime($time) + ( $timezone/-1)*60*60 ;  // Get GMT time 
	$timezoneoffset=$GMTClient* 60*60; // Get offset between local and GMT 
	$local_time=$gmt+$timezoneoffset ; // Add offset to get local time  
	
	return date("Y-m-d H:i:s", $local_time);

}

function roundNearestHalf ($average)
{
	$round_to = 0.5;
	$rounded = round($average / $round_to) * $round_to;
	return $rounded;
}

function isSopcastChannel($channel)
{
	$pos = strpos(strtolower($channel) , "sopcast" );
	if ( $pos !== false )
		return true;
	else return false;	

}
function isSopcastLink($url)
{
	$pos = strpos(strtolower($url) , "sop" );
	if ( $pos !== false )
		return true;
	else return false;	
}

function getTeamId($name)
{
	$name = trim($name);
	if($name=="")
		return;
	
	$sql = "select id from team where name='".addslashes($name)."'";
	$team=db_fetch_one($sql);
	if($team)
		return $team->id;
	else
	{
		$data["name"] =  $name;
		return db_insert("team",$data);
	}
}
function getTeamLogo($id)
{
	$sql = "select logo from team where id=".addslashes($id);
	$team=db_fetch_one($sql);
	
	if($team)
		return $team->logo;
}
?>